﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="UserRole.aspx.cs" Inherits="WebApplication.Admin.UserRole" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script src="../../scripts/boot.js" type="text/javascript"></script>
</head>
<body>
    <div id="rootPanel" class="mini-splitter" style="width: 100%; height: 100%;">
        <div size="300" showcollapsebutton="true">
            <div class="mini-fit">
                <ul id="tree1" class="mini-tree" url="/Handler.ashx?method=LoadRole&mode=tree" style="width: 100%;
                    height: 100%; padding: 5px;" showtreeicon="true" textfield="Name" expandonload="0"
                    allowdrag="true" allowdrop="true" allowleafdropin="true" idfield="ID" parentfield="PID"
                    resultastree="false">
                </ul>
            </div>
        </div>
        <div showcollapsebutton="false">
            <div class="mini-toolbar" style="padding: 2px; border-top: 0; border-left: 0; border-right: 0;">
                <table style="width: 100%;">
                    <tr>
                        <td>
                            <input id="key" class="mini-textbox" emptytext="输入帐号、姓名、部门、职位等关键字进行查询" style="width: 280px;
                                margin-right: 10px;" onenter="onSearchEnter" />
                            <a class="mini-button" onclick="searchList()" iconcls="icon-search" plain="false">查询</a><span
                                class="separator"></span> <a class="mini-button" iconcls="icon-add" onclick="addRow()">
                                    分配角色</a><span class="separator"></span> <a class="mini-button" iconcls="icon-remove"
                                        onclick="mini.confirm('数据删除后不可恢复，确认要删除选中行吗？','警告',function(){removeRow();})">删除</a>
                        </td>
                    </tr>
                </table>
            </div>
            <div class="mini-fit">
                <div id="grid1" class="mini-datagrid" style="width: 100%; height: 100%;" borderstyle="border:0;"
                    url="/Handler.ashx?method=GetUserByRoleID" allowcellselect="true" allowcelledit="false"
                    multiselect="true" allowresize="true" allowcellvalid="true" oncellvalidation=""
                    pagesize="15">
                    <div property="columns">
                        <div type="checkcolumn">
                        </div>
                        <div type="indexcolumn" headeralign="center" allowsort="true">
                            序号</div>
                        <div field="Account" allowsort="true" headeralign="center" width="100" cellstyle="background-color:#FFFFE6">
                            帐号
                        </div>
                        <div field="Name" allowsort="true" headeralign="center" width="40" cellstyle="background-color:#FFFFE6">
                            姓名
                        </div>
                        <div field="Department" allowsort="true" headeralign="center" cellstyle="background-color:#FFFFE6">
                            部门
                        </div>
                        <div field="Title" allowsort="true" headeralign="center" width="250">
                            职位
                        </div>
                        <div name="edit" field="Account" width="80" align="center" headeralign="center">
                            操作
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <script type="text/javascript">
        mini.parse();
        var tree = mini.get("tree1");
        var grid = mini.get("grid1");
        grid.on("drawcell", function (e) {
            var column = e.column;
            if (column.name == "edit") {
                e.cellStyle = "text-align:center";
                e.cellHtml = "<a href='javascript:void(0)' onclick='deleteRow(\"" + grid.getRow(e.rowIndex).ID + "\")'>删除</a>";
            }
        });
        tree.on("drawnode", function (e) { e.nodeHtml = "<span title='" + e.node.Description + "'>" + e.node.Name + "<span>"; });

        function onShowRowDetail(e) {
            var grid = e.sender;
            var row = e.record;

            var td = grid.getRowDetailCellEl(row);

            td.appendChild(detailGrid_Form);
            detailGrid_Form.style.display = "block";
            OrgaPost_grid.load({ UserAccount: row.UserAccount });
        }

        tree.on("nodeselect", function (e) {
            loadlist("true");
        });

        //查询列表触发事件
        function searchList() {
            loadlist();
        }

        //查询列表回车事件
        function onSearchEnter(e) {
            searchList();
        }

        //查询列表事件
        function loadlist() {
            grid.load({ RID: tree.getSelectedNode().ID, Key: mini.get("key").getValue() });
        }

        function addRow() {
            if (tree.getSelectedNode() == null || tree.getSelectedNode().Name == "所有角色") {
                mini.alert("请在左侧选择角色！"); return;
            }
            mini.open({
                url: "/Admin/UserSelect.aspx",
                title: "选择用户", width: 800, height: 640,
                ondestroy: function (action) {
                    if (action == "ok") {
                        mini.get("rootPanel").loading();
                        var iframe = this.getIFrameEl();
                        var datas = iframe.contentWindow.GetData();
                        var accounts = new Array();
                        for (var i = 0; i < datas.length; i++) {
                            accounts.push(datas[i].Account);
                        }
                        $.ajax({
                            url: "/Handler.ashx?method=SaveUserRole",
                            data: { data: mini.encode({ RID: tree.getSelectedNode().ID, Accounts: accounts }) },
                            type: "post",
                            success: function (text) {
                                mini.get("rootPanel").unmask(); var result = mini.decode(text);
                                if (result.Success) { grid.reload(); } else { mini.alert(result.Message); }
                            },
                            error: function (jqXHR, textStatus, errorThrown) {
                                mini.alert(jqXHR.responseText); mini.get("rootPanel").loading();
                            }
                        });
                    }
                }
            });
        }

        function deleteRow(id) {
            delUserRole(id.toString());
        }

        function removeRow() {
            mini.get("rootPanel").loading();
            var rows = grid.getSelecteds();
            if (rows.length > 0) {
                var ids = "";
                for (var i = 0; i < rows.length; i++) {
                    ids += rows[i].ID + ",";
                }
                delUserRole(ids);
            }
            mini.get("rootPanel").unmask();
        }

        function delUserRole(ids) {
            $.ajax({
                url: "/handler.ashx?method=DelUserRole",
                data: { ids: ids },
                type: "post",
                success: function (text) {
                    var result = mini.decode(text);
                    if (result.Success) {
                        grid.reload();
                    } else {
                        mini.alert(result.Message)
                    }
                },
                error: function (jqXHR, textStatus, errorThrown) {
                    mini.alert(jqXHR.responseText);
                }
            });
        }
    </script>
</body>
</html>
